<template>
  <!-- 主体区域 -->
  <section id="app">
    <AppHeader @add="handleAdd"></AppHeader>
    <AppMain @del="handledel" :list="list"></AppMain>
    <AppFooter @clear="handleClear" :list="list"></AppFooter>
  </section>
</template>

<script>
import AppFooter from "./components/AppFooter.vue";
import AppHeader from "./components/AppHeader.vue";
import AppMain from "./components/AppMain.vue";

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, name: "打篮球" },
        { id: 2, name: "看电影" },
        { id: 3, name: "学习前端" },
      ],
    };
  },
  methods: {
    handleAdd(todoname) {
      this.list.unshift({
        id: +new Date(),
        name: todoname,
      });
      // console.log(todoname);
    },
    handledel(id) {
      // console.log(id);
      this.list = this.list.filter((item) => item.id !== id);
    },
    handleClear() {
      this.list = [];
    },
  },
  components: {
    AppHeader,
    AppMain,
    AppFooter,
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        localStorage.setItem("list", JSON.stringify(newValue));
      },
    },
  },
};
</script>

<style>
</style>
